package com.sword.service.impl.sale;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.pojo.sale.Customer;
import com.sword.pojo.stock.product.Productclass;
import com.sword.service.impl.BasicBaseServiceImpl;
import com.sword.service.sale.CustomerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;

@Service
@Transactional
public class CustomerServiceImpl extends BasicBaseServiceImpl<Customer> implements CustomerService {
    @Override
    public PageResult queryCustomer(QueryPageBean queryPageBean) {
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        // 如果queryString有值
        if (queryString != null) {
            Customer customer = new Customer();
            Field[] fields = customer.getClass().getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                //有的字段是用private修饰的 将他设置为可读
                fields[i].setAccessible(true);
                if(fields[i].getName().endsWith("Date")){
                    continue;
                }
                wrapper.like(fields[i].getName(),queryString).or();
            }
        }
        IPage<Customer> ipage = super.queryPageList(wrapper,queryPageBean.getPage(),queryPageBean.getLimit());
        return new PageResult(0, MessageConstant.QUERY_CUSTOMER_SUCCESS, ipage.getTotal(), ipage.getRecords());
    }

    @Override
    public Integer addCustomer(Customer customer) {
        return super.save(customer);
    }

    @Override
    public Integer editCustomer(Customer customer) {
        return super.update(customer);
    }

    @Override
    public Integer deleteCustomer(Integer id) {
        return super.deleteById(Long.valueOf(id));
    }
}
